home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / oper_sys / kerberos / pc / krb_libk.lha / Lib / KRB / KNM_PARS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1991-07-24  |  5.4 KB  |  237 lines

  1. /*
  2.  * $Source: /mit/kerberos/src/lib/krb/RCS/kname_parse.c,v $
  3.  * $Author: jtkohl $
  4.  *
  5.  * Copyright 1987, 1988 by the Massachusetts Institute of Technology.
  6.  *
  7.  * For copying and distribution information, please see the file
  8.  * <mit-copyright.h>.
  9.  */
  10.  
  11. #ifndef lint
  12. static char rcsid_kname_parse_c[] =
  13. "$Header: kname_parse.c,v 4.4 88/12/01 14:07:29 jtkohl Exp $";
  14. #endif /* lint */
  15.  
  16. #include <mit_copy.h>
  17.  
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <krb.h>
  21.  
  22. /* max size of full name */
  23. #define FULL_SZ (ANAME_SZ + INST_SZ + REALM_SZ)
  24.  
  25. #define NAME    0        /* which field are we in? */
  26. #define INST    1
  27. #define REALM   2
  28.  
  29. extern char *krb_err_txt[];
  30.  
  31. /*
  32.  * This file contains four routines for handling Kerberos names.
  33.  *
  34.  * kname_parse() breaks a Kerberos name into its name, instance,
  35.  * and realm components.
  36.  *
  37.  * k_isname(), k_isinst(), and k_isrealm() check a given string to see if
  38.  * it's a syntactically legitimate respective part of a Kerberos name,
  39.  * returning 1 if it is, 0 if it isn't.
  40.  *
  41.  * Definition of "syntactically legitimate" names is according to
  42.  * the Project Athena Technical Plan Section E.2.1, page 7 "Specifying
  43.  * names", version dated 21 Dec 1987.
  44.  * /
  45.  
  46. /*
  47.  * kname_parse() takes a Kerberos name "fullname" of the form:
  48.  *
  49.  *        username[.instance][@realm]
  50.  *
  51.  * and returns the three components ("name", "instance", and "realm"
  52.  * in the example above) in the given arguments "np", "ip", and "rp".
  53.  *
  54.  * If successful, it returns KSUCCESS.  If there was an error,
  55.  * KNAME_FMT is returned.
  56.  */
  57.  
  58. kname_parse(np, ip, rp, fullname)
  59.     char *np, *ip, *rp, *fullname;
  60. {
  61.     static char buf[FULL_SZ];
  62.     char *rnext, *wnext;    /* next char to read, write */
  63.     register char c;
  64.     int backslash;
  65.     int field;
  66.  
  67.     backslash = 0;
  68.     rnext = buf;
  69.     wnext = np;
  70.     field = NAME;
  71.  
  72.     if (strlen(fullname) > FULL_SZ)
  73.         return KNAME_FMT;
  74.     (void) strcpy(buf, fullname);
  75.  
  76.     while (c = *rnext++) {
  77.         if (backslash) {
  78.             *wnext++ = c;
  79.             backslash = 0;
  80.             continue;
  81.         }
  82.         switch (c) {
  83.         case '\\':
  84.             backslash++;
  85.             break;
  86.         case '.':
  87.             switch (field) {
  88.             case NAME:
  89.                 if (wnext == np)
  90.                     return KNAME_FMT;
  91.                 *wnext = '\0';
  92.                 field = INST;
  93.                 wnext = ip;
  94.                 break;
  95.             case INST:
  96.                 return KNAME_FMT;
  97.                 /* break; */
  98.             case REALM:
  99.                 *wnext++ = c;
  100.                 break;
  101.             default:
  102.                 fprintf(stderr, "unknown field value\n");
  103.                 exit(1);
  104.             }
  105.             break;
  106.         case '@':
  107.             switch (field) {
  108.             case NAME:
  109.                 if (wnext == np)
  110.                     return KNAME_FMT;
  111.                 *ip = '\0';
  112.                 /* fall through */
  113.             case INST:
  114.                 *wnext = '\0';
  115.                 field = REALM;
  116.                 wnext = rp;
  117.                 break;
  118.             case REALM:
  119.                 return KNAME_FMT;
  120.             default:
  121.                 fprintf(stderr, "unknown field value\n");
  122.                 exit(1);
  123.             }
  124.             break;
  125.         default:
  126.             *wnext++ = c;
  127.         }
  128.     }
  129.     *wnext = '\0';
  130.     if ((strlen(np) > ANAME_SZ - 1) ||
  131.         (strlen(ip) > INST_SZ  - 1) ||
  132.         (strlen(rp) > REALM_SZ - 1))
  133.         return KNAME_FMT;
  134.     return KSUCCESS;
  135. }
  136.  
  137. /*
  138.  * k_isname() returns 1 if the given name is a syntactically legitimate
  139.  * Kerberos name; returns 0 if it's not.
  140.  */
  141.  
  142. k_isname(s)
  143.     char *s;
  144. {
  145.     register char c;
  146.     int backslash = 0;
  147.  
  148.     if (!*s)
  149.         return 0;
  150.     if (strlen(s) > ANAME_SZ - 1)
  151.         return 0;
  152.     while(c = *s++) {
  153.         if (backslash) {
  154.             backslash = 0;
  155.             continue;
  156.         }
  157.         switch(c) {
  158.         case '\\':
  159.             backslash = 1;
  160.             break;
  161.         case '.':
  162.             return 0;
  163.             /* break; */
  164.         case '@':
  165.             return 0;
  166.             /* break; */
  167.         }
  168.     }
  169.     return 1;
  170. }
  171.  
  172.  
  173. /*
  174.  * k_isinst() returns 1 if the given name is a syntactically legitimate
  175.  * Kerberos instance; returns 0 if it's not.
  176.  */
  177.  
  178. k_isinst(s)
  179.     char *s;
  180. {
  181.     register char c;
  182.     int backslash = 0;
  183.  
  184.     if (strlen(s) > INST_SZ - 1)
  185.         return 0;
  186.     while(c = *s++) {
  187.         if (backslash) {
  188.             backslash = 0;
  189.             continue;
  190.         }
  191.         switch(c) {
  192.         case '\\':
  193.             backslash = 1;
  194.             break;
  195.         case '.':
  196.             return 0;
  197.             /* break; */
  198.         case '@':
  199.             return 0;
  200.             /* break; */
  201.         }
  202.     }
  203.     return 1;
  204. }
  205.  
  206. /*
  207.  * k_isrealm() returns 1 if the given name is a syntactically legitimate
  208.  * Kerberos realm; returns 0 if it's not.
  209.  */
  210.  
  211. k_isrealm(s)
  212.     char *s;
  213. {
  214.     register char c;
  215.     int backslash = 0;
  216.  
  217.     if (!*s)
  218.         return 0;
  219.     if (strlen(s) > REALM_SZ - 1)
  220.         return 0;
  221.     while(c = *s++) {
  222.         if (backslash) {
  223.             backslash = 0;
  224.             continue;
  225.         }
  226.         switch(c) {
  227.         case '\\':
  228.             backslash = 1;
  229.             break;
  230.         case '@':
  231.             return 0;
  232.             /* break; */
  233.         }
  234.     }
  235.     return 1;
  236. }
  237.